package reduce.fz.com.reduce.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.bean.StoryCard;
import reduce.fz.com.reduce.bean.StoryPicCard;
import reduce.fz.com.reduce.custom.HeaderFooterStatusRecyclerViewAdapter;
import reduce.fz.com.reduce.custom.NetworkImageView;
import reduce.fz.com.reduce.custom.SharePubBtn;
import reduce.fz.com.reduce.util.HttpUtils;

/**
 * Created by qingshengzheng on 15/10/14.
 */
public class PubStoryAdapter extends HeaderFooterStatusRecyclerViewAdapter<BaseRecycleViewHolder> {
    private List<StoryPicCard> storyPicCardList = new ArrayList<StoryPicCard>();
    private List<String> localPaths;
    private StoryCard storyCard = new StoryCard();
    private LayoutInflater layoutInflater;
    private EditText etStoryTitle;
    private Context context;
    private OnPicItemListener picItemListener;

    public PubStoryAdapter(Context context, OnPicItemListener picItemListener) {
        this(context);
        this.picItemListener = picItemListener;
    }


    public interface OnPicItemListener {

        public void onSelectImage(int position, StoryPicCard card);

        public void onEditComment(int position, StoryPicCard card);
    }

    /**
     * 传入的本地图片路径初始化
     *
     * @param localPaths
     */
    public void init(List<String> localPaths) {
        storyPicCardList.clear();
        for (int i = 0; i < localPaths.size(); i++) {
            StoryPicCard picCard = new StoryPicCard();
            picCard.localpth = localPaths.get(i);
            storyPicCardList.add(picCard);

        }
        notifyDataSetChanged();
    }

    /**
     * 在前方添加一个
     */
    public void addBefore(int position) {
        StoryPicCard card = new StoryPicCard();
        storyPicCardList.add(position, card);
        notifyContentItemInserted(position);
    }

    /**
     * 在后放添加一个
     *
     * @param position
     */
    public void addAfter(int position) {
        StoryPicCard card = new StoryPicCard();
        storyPicCardList.add(position + 1, card);
        notifyContentItemInserted(position + 1);
    }

    /**
     * 删除当前位置
     *
     * @param position
     */
    public void delCurrent(int position) {
        storyPicCardList.remove(position);
        notifyContentItemRemoved(position);
    }

    /**
     * 和前面一个互换
     *
     * @param position
     */
    public void switchWithBefore(int position) {
        if (position == 0) return;
        StoryPicCard p = storyPicCardList.get(position);
        StoryPicCard b = storyPicCardList.get(position - 1);
        storyPicCardList.set(position, b);
        storyPicCardList.set(position - 1, p);
        notifyContentItemRangeChanged(position - 1, 2);
    }

    /**
     * 后后面一个互换
     *
     * @param position
     */
    public void switchWithAfter(int position) {
        if (position == storyPicCardList.size() - 1) return;
        StoryPicCard p = storyPicCardList.get(position);
        StoryPicCard a = storyPicCardList.get(position + 1);
        storyPicCardList.set(position, a);
        storyPicCardList.set(position + 1, p);
        notifyContentItemRangeChanged(position, 2);
    }


    /**
     * 初始化
     *
     * @param context
     */
    public PubStoryAdapter(Context context) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    /**
     * 获取story图片card
     *
     * @return
     */
    public List<StoryPicCard> getStoryPicCard() {

        return storyPicCardList;
    }

    /**
     * 返回分享故事card卡片
     *
     * @return
     */
    public StoryCard getStoryCard() {
        this.storyCard.picCards = storyPicCardList;
        this.storyCard.title = etStoryTitle.getText().toString();
        return this.storyCard;
    }

    /**
     * 加载storycard
     *
     * @param cards
     */
    public void addStoryPicCard(List<StoryPicCard> cards) {
        this.storyPicCardList = cards;
        int start = storyPicCardList.size();
        int len = cards.size();
        notifyContentItemRangeInserted(start, len);
    }

    public class PubHeaderHolder extends BaseRecycleViewHolder {


        public PubHeaderHolder(View itemView) {
            super(itemView);
            initWidget(itemView);
        }

        public void initWidget(View view) {
            etStoryTitle = (EditText) view.findViewById(R.id.et_story_title);
        }

        /**
         * 获取编辑框的内容
         *
         * @return
         */
        public String getStoryTitle() {
            return etStoryTitle.getText().toString();
        }

        @Override
        public void renderView(Object data, int position) {
        }
    }


    public void replaceImages() {

    }


    /**
     * 分享图片content holder
     */
    public class PubImageCardViewHolder extends BaseRecycleViewHolder<StoryPicCard> implements View.OnClickListener {
        //删除
        private ImageView mIvStoryPicDel;
        //在上方添加
        private ImageView mIvStoryPicAddUp;
        //图片选择布局
        private RelativeLayout mRlSelectPhoto;
        //图片选择
        private ImageView mIvSelectPhoto;
        //图片文本内容
        private TextView mTvStoryPicComments;
        //替换-上方
        private ImageView mIvStoryPicSwitchUp;
        //替换-下方
        private ImageView mIvStoryPicSwitchDown;
        //在下方添加
        private ImageView mIvStoryPicAddDown;


        private StoryPicCard storyPicCard;

        public void initWidget(View view) {
            mIvStoryPicDel = (ImageView) view.findViewById(R.id.iv_story_pic_del);
            mIvStoryPicAddUp = (ImageView) view.findViewById(R.id.iv_story_pic_add_up);
            mRlSelectPhoto = (RelativeLayout) view.findViewById(R.id.rl_select_photo);
            mIvSelectPhoto = (ImageView) view.findViewById(R.id.iv_select_photo);
            mTvStoryPicComments = (TextView) view.findViewById(R.id.tv_story_pic_comments);
            mIvStoryPicSwitchUp = (ImageView) view.findViewById(R.id.iv_story_pic_switch_up);
            mIvStoryPicSwitchDown = (ImageView) view.findViewById(R.id.iv_story_pic_switch_down);
            mIvStoryPicAddDown = (ImageView) view.findViewById(R.id.iv_story_pic_add_down);
            mIvSelectPhoto.setOnClickListener(this);
            mTvStoryPicComments.setOnClickListener(this);
            mIvStoryPicSwitchDown.setOnClickListener(this);
            mIvStoryPicSwitchUp.setOnClickListener(this);
            mIvStoryPicAddDown.setOnClickListener(this);
            mIvStoryPicDel.setOnClickListener(this);
            mIvStoryPicAddUp.setOnClickListener(this);
            mRlSelectPhoto.setOnClickListener(this);
        }

        public int getIndex() {
            int index= storyPicCardList.indexOf(storyPicCard);
            return index;
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.iv_story_pic_del:
                    delCurrent(getIndex());
                    break;
                case R.id.iv_story_pic_add_up:
                    addBefore(getIndex());
                    break;
                case R.id.iv_story_pic_add_down:
                    addAfter(getIndex());
                    break;
                case R.id.rl_select_photo:
                case R.id.iv_select_photo:
                    picItemListener.onSelectImage(getIndex(), storyPicCard);
                    break;
                case R.id.tv_story_pic_comments:
                    picItemListener.onEditComment(getIndex(), storyPicCard);
                    break;
                case R.id.iv_story_pic_switch_down:
                    switchWithAfter(getIndex());
                    break;
                case R.id.iv_story_pic_switch_up:
                    switchWithBefore(getIndex());
                    break;
                default:
                    break;
            }

        }

        public PubImageCardViewHolder(View itemView) {
            super(itemView);
            initWidget(itemView);
        }

        public void initData(StoryPicCard card) {
            HttpUtils.getSDCardImageLoader().loadImage(5, card.localpth, mIvSelectPhoto);
            mTvStoryPicComments.setText(card.desc);
        }

        @Override
        public void renderView(StoryPicCard data, int position) {
            this.storyPicCard = data;
            int index=getIndex();
            if (index == storyPicCardList.size() - 1)
                mIvStoryPicAddDown.setVisibility(View.VISIBLE);
            else mIvStoryPicAddDown.setVisibility(View.GONE);

            initData(data);


        }


    }

    @Override
    public BaseRecycleViewHolder createFooterStatusViewHolder(View footerView) {
        return new BaseRecycleViewHolder(footerView) {
            @Override
            public void renderView(Object data, int position) {

            }
        };
    }

    @Override
    protected int getHeaderItemCount() {
        return 1;
    }

    @Override
    protected int getContentItemCount() {
        return storyPicCardList.size();
    }

    @Override
    protected BaseRecycleViewHolder onCreateHeaderItemViewHolder(ViewGroup parent, int headerViewType) {
        View view = layoutInflater.inflate(R.layout.view_pub_story_header, parent, false);

        return new PubHeaderHolder(view);
    }

    @Override
    protected BaseRecycleViewHolder onCreateContentItemViewHolder(ViewGroup parent, int contentViewType) {
        View view = layoutInflater.inflate(R.layout.pub_story_card_item_view, parent, false);
        return new PubImageCardViewHolder(view);
    }

    @Override
    protected void onBindHeaderItemViewHolder(BaseRecycleViewHolder headerViewHolder, int position) {
        PubHeaderHolder holder = (PubHeaderHolder) headerViewHolder;

    }

    @Override
    protected void onBindContentItemViewHolder(BaseRecycleViewHolder contentViewHolder, int position) {
        PubImageCardViewHolder holder = (PubImageCardViewHolder) contentViewHolder;
        holder.renderView(storyPicCardList.get(position), position);


    }


}
